package manage.action;

import m.common.action.ActionMeta;
import m.common.action.RequestMeta;
import m.common.action.VuePageResult;
import m.common.action.VueResult;
import m.common.model.util.ModelQueryList;
import m.common.model.util.QueryCondition;
import m.common.model.util.QueryOrder;
import m.system.exception.MException;
import m.system.util.JSONMessage;
import m.system.util.StringUtil;
import manage.model.MenuInfo;
import manage.model.ModuleInfo;
import manage.service.ModuleService;

@ActionMeta(name="manage/module")
public class ModuleAction extends ManageAction {
    private ModuleInfo module;
    private MenuInfo menu;

    public String getPowerName() {
        return "manage_api_power";
    }
    public JSONMessage getModuleList(){
        JSONMessage result=new JSONMessage();
        try {
            verifyAdminOperPower(getPowerName());
            result.push("list", ModelQueryList.getModelList(ModuleInfo.class,new String[]{"*"},null,null, QueryOrder.asc("sort")));
            fillJSONResult(result,true,"");
        } catch (Exception e) {
            fillJSONResult(result,false,e.getMessage());
        }
        return result;
    }
    @RequestMeta(params = {"oid"})
    public JSONMessage getMenuList(String oid){
        JSONMessage result=new JSONMessage();
        try {
            verifyAdminOperPower(getPowerName());
            result.push("list", ModelQueryList.getModelList(MenuInfo.class,new String[]{"*"},null,
                QueryCondition.or(
                    QueryCondition.and(
                        QueryCondition.eq("moduleInfo.oid",oid),
                        QueryCondition.isEmpty("parentMenu.oid")
                    ),
                    QueryCondition.eq("parentMenu.oid",oid)
                ),
                QueryOrder.asc("sort")
            ));
            fillJSONResult(result,true,"");
        } catch (Exception e) {
            fillJSONResult(result,false,e.getMessage());
        }
        return result;
    }
    public JSONMessage doSaveModule(){
        JSONMessage result=new JSONMessage();
        try {
            verifyAdminOperPower(getPowerName());
            String msg=getService(ModuleService.class).saveModule(module);
            fillJSONResult(result,true,msg);
        } catch (Exception e) {
            fillJSONResult(result,false,e.getMessage());
        }
        return result;
    }
    public VueResult toEditModule() throws Exception {
        verifyAdminOperPower(getPowerName());
        if(null!=module&&!StringUtil.isSpace(module.getOid())){
            module=ModelQueryList.getModel(module,"*");
        }else{
            module=new ModuleInfo();
            module.setIsInput("Y");
            module.setIsPublic("N");
            module.setType("A");
        }
        return new VuePageResult("page/base/module/editModule.js")
            .addParam("model",module);
    }
    public JSONMessage doSaveMenu(){
        JSONMessage result=new JSONMessage();
        try {
            verifyAdminOperPower(getPowerName());
            String msg=getService(ModuleService.class).saveMenu(menu);
            fillJSONResult(result,true,msg);
        } catch (Exception e) {
            fillJSONResult(result,false,e.getMessage());
        }
        return result;
    }
    public VueResult toEditMenu() throws Exception {
        verifyAdminOperPower(getPowerName());
        if(null!=menu&&!StringUtil.isSpace(menu.getOid())){
            menu=ModelQueryList.getModel(menu,"*");
        }else if(null!=menu&&(
                null!=menu.getModuleInfo()&&!StringUtil.isSpace(menu.getModuleInfo().getOid())
                ||null!=menu.getParentMenu()&&!StringUtil.isSpace(menu.getParentMenu().getOid())
            )){
            menu.setIsInput("Y");
            menu.setIsPublic("N");
        }else{
            throw new MException(this.getClass(),"参数错误");
        }
        return new VuePageResult("page/base/module/editMenu.js")
            .addParam("model",menu);
    }

    public ModuleInfo getModule() {
        return module;
    }

    public void setModule(ModuleInfo module) {
        this.module = module;
    }

    public MenuInfo getMenu() {
        return menu;
    }

    public void setMenu(MenuInfo menu) {
        this.menu = menu;
    }
}
